package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctInstrumentComparisonItemsBo;
import com.rzdata.poct.system.domain.business.po.PoctInstrumentComparisonItems;
import com.rzdata.poct.system.domain.business.vo.PoctInstrumentComparisonItemsVo;
import com.rzdata.poct.system.mapper.PoctInstrumentComparisonItemsMapper;
import com.rzdata.poct.system.service.IPoctInstrumentComparisonItemsService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备一致性比对Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-17
 */
@Service
public class PoctInstrumentComparisonItemsServiceImpl extends ServicePlusImpl<PoctInstrumentComparisonItemsMapper, PoctInstrumentComparisonItems, PoctInstrumentComparisonItemsVo> implements IPoctInstrumentComparisonItemsService {

    @Override
    public PoctInstrumentComparisonItemsVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctInstrumentComparisonItemsVo> queryPageList(PoctInstrumentComparisonItemsBo bo) {
        PagePlus<PoctInstrumentComparisonItems, PoctInstrumentComparisonItemsVo> result
            = pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctInstrumentComparisonItemsVo> queryList(PoctInstrumentComparisonItemsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctInstrumentComparisonItems> buildQueryWrapper(PoctInstrumentComparisonItemsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctInstrumentComparisonItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getComparisonId() != null, PoctInstrumentComparisonItems::getComparisonId, bo.getComparisonId());
        lqw.eq(StringUtils.isNotBlank(bo.getSampelNo()), PoctInstrumentComparisonItems::getSampelNo, bo.getSampelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandOne()), PoctInstrumentComparisonItems::getBrandOne, bo.getBrandOne());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandTwo()), PoctInstrumentComparisonItems::getBrandTwo, bo.getBrandTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), PoctInstrumentComparisonItems::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getBiasValue()), PoctInstrumentComparisonItems::getBiasValue, bo.getBiasValue());
        lqw.eq(StringUtils.isNotBlank(bo.getBiasR()), PoctInstrumentComparisonItems::getBiasR, bo.getBiasR());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctInstrumentComparisonItemsBo bo) {
        PoctInstrumentComparisonItems add = BeanUtil.toBean(bo, PoctInstrumentComparisonItems.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctInstrumentComparisonItemsBo bo) {
        PoctInstrumentComparisonItems update = BeanUtil.toBean(bo, PoctInstrumentComparisonItems.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctInstrumentComparisonItems entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
